package com.skp.shaphan.ui.windows;

import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.Select;

public class ParseSqlWindow extends JFrame {

	private JTextArea sql;
	private JTextArea parseText;
	
	private String sqlType;
	private String errorMessage;
	private ArrayList<Table> tables;

	public ParseSqlWindow() {
		super("Parse SQL");
		setSize(500, 500);
		JTabbedPane tabs = new JTabbedPane();
		sql = new JTextArea();
		tabs.add("Original SQL", sql);
		parseText = new JTextArea();
		tabs.add("Parse Text", parseText);
		add(tabs);
		
		tables = new ArrayList<Table>();
	}
	
	public void setSQL(String sql) {
		this.sql.setText(sql);
	}
	
	public void setSQLType(String type) {
		this.sqlType = type;
		updateParseText();
	}
	
	public void updateParseText() {
		StringBuilder text = new StringBuilder();
		text.append("Type: ");
		text.append(sqlType);
		text.append("\n\nTables");
		for(Table table : tables) {
			text.append("\n  -");
			text.append(table.getName());
			text.append(" (");
			text.append(table.getAlias());
			text.append(")");
		}
		if(errorMessage != null) {
			text.append("\n\nError: " + errorMessage);
		}
		
		parseText.setText(text.toString());
	}

	public void setError(String error) {
		this.errorMessage = error;
	}

	public void addTable(Table tableName) {
		tables.add(tableName);
		updateParseText();
	}

}
